package com.datastructures2.graph.有向图;

import com.datastructures.graph.Bag;

/**
 * 有向图数据类型API
 * @author MaoLin Wang
 * @date 2020/2/2210:19
 */
public class Digraph {
    private final int V;
    private int E;
    private Bag<Integer>[] adj;

    public Digraph(int v) {
        V = v;
        this.E=0;
        adj=new Bag[V];
        for (int i = 0; i < v; i++) {
            adj[i]=new Bag<>();
        }
    }
    public int V(){
        return V;
    }
    public int E(){
        return E;
    }
    public void addEdge(int v,int w){
        adj[v].add(w);
        E++;
    }
    public Iterable<Integer> adj(int v){
        return adj[v];
    }
    public Digraph reverse(){
        Digraph R=new Digraph(V);
        for (int v = 0; v < V; v++) {
            for (int w: adj(v)){
                R.addEdge(w,v);
            }
        }
        return R;
    }
}
